{
GList *l, *files;
GFile *file;
- const char *uri;
files = NULL;
for (l = hits; l; l = l->next)
{
- uri = (const gchar *)l->data;
- file = g_file_new_for_uri (uri);
+ GtkSearchHit *hit = (GtkSearchHit *)l->data;
+ file = g_file_new_for_uri (hit->uri);
if (!file)
continue;
files = g_list_prepend (files, file);
{
GtkSearchEngine *composite = GTK_SEARCH_ENGINE (data);
GList *added, *l;
+ GtkSearchHit *hit;
added = NULL;
for (l = hits; l; l = l->next)
{
- gchar *hit = l->data;
+ hit = l->data;
if (!g_hash_table_contains (composite->priv->hits, hit))
{
- hit = g_strdup (hit);
+ hit = _gtk_search_hit_dup (hit);
g_hash_table_add (composite->priv->hits, hit);
added = g_list_prepend (added, hit);
}
update_status (composite);
}
+static gboolean
+search_hit_equal (gconstpointer a, gconstpointer b)
+{
+ const GtkSearchHit *ha = (const GtkSearchHit *)a;
+ const GtkSearchHit *hb = (const GtkSearchHit *)b;
+
+ return g_str_equal (ha->uri, hb->uri);
+}
+
+
+static guint
+search_hit_hash (gconstpointer a)
+{
+ const GtkSearchHit *ha = (const GtkSearchHit *)a;
+
+ return g_str_hash (ha->uri);
+}
+
+GtkSearchHit *
+_gtk_search_hit_dup (GtkSearchHit *hit)
+{
+ GtkSearchHit *dup;
+
+ dup = g_new (GtkSearchHit, 1);
+ dup->uri = g_strdup (hit->uri);
+ if (hit->info)
+ dup->info = g_object_ref (hit->info);
+ else
+ dup->info = NULL;
+
+ return dup;
+}
+
+void
+_gtk_search_hit_free (GtkSearchHit *hit)
+{
+ g_free (hit->uri);
+ g_clear_object (&hit->info);
+ g_free (hit);
+}
+
static void
connect_engine_signals (GtkSearchEngine *engine,
gpointer data)
g_debug ("Using simple search engine");
connect_engine_signals (engine->priv->simple, engine);
- engine->priv->hits = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+ engine->priv->hits = g_hash_table_new_full (search_hit_hash, search_hit_equal,
+ (GDestroyNotify)_gtk_search_hit_free, NULL);
return engine;
}
#define __GTK_SEARCH_ENGINE_H__
#include "gtkquery.h"
+#include <gio/gio.h>
G_BEGIN_DECLS
typedef struct _GtkSearchEngineClass GtkSearchEngineClass;
typedef struct _GtkSearchEnginePrivate GtkSearchEnginePrivate;
+typedef struct _GtkSearchHit GtkSearchHit;
+
+struct _GtkSearchHit
+{
+ gint ref_count;
+ gchar *uri;
+ GFileInfo *info; /* may be NULL */
+};
+
struct _GtkSearchEngine
{
GObject parent;
void _gtk_search_engine_error (GtkSearchEngine *engine,
const gchar *error_message);
+void _gtk_search_hit_free (GtkSearchHit *hit);
+GtkSearchHit *_gtk_search_hit_dup (GtkSearchHit *hit);
+
G_END_DECLS
#endif /* __GTK_SEARCH_ENGINE_H__ */
typedef struct
{
- GList *uris;
+ GList *hits;
SearchThreadData *thread_data;
-} SearchHits;
+} Batch;
static gboolean
search_thread_add_hits_idle (gpointer user_data)
{
- SearchHits *hits = user_data;
+ Batch *batch = user_data;
- if (!g_cancellable_is_cancelled (hits->thread_data->cancellable))
- _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine), hits->uris);
+ if (!g_cancellable_is_cancelled (batch->thread_data->cancellable))
+ _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (batch->thread_data->engine), batch->hits);
- g_list_free_full (hits->uris, g_free);
- g_free (hits);
+ g_list_free_full (batch->hits, (GDestroyNotify)_gtk_search_hit_free);
+ g_free (batch);
return FALSE;
}
static void
send_batch (SearchThreadData *data)
{
- SearchHits *hits;
+ Batch *batch;
data->n_processed_files = 0;
{
guint id;
- hits = g_new (SearchHits, 1);
- hits->uris = data->hits;
- hits->thread_data = data;
+ batch = g_new (Batch, 1);
+ batch->hits = data->hits;
+ batch->thread_data = data;
- id = gdk_threads_add_idle (search_thread_add_hits_idle, hits);
+ id = gdk_threads_add_idle (search_thread_add_hits_idle, batch);
g_source_set_name_by_id (id, "[gtk+] search_thread_add_hits_idle");
}
continue;
if (gtk_query_matches_string (data->query, display_name))
- data->hits = g_list_prepend (data->hits, g_file_get_uri (child));
+ {
+ GtkSearchHit *hit;
+
+ hit = g_new (GtkSearchHit, 1);
+ hit->uri = g_file_get_uri (child);
+ hit->info = g_object_ref (info);
+ data->hits = g_list_prepend (data->hits, hit);
+ }
data->n_processed_files++;
if (data->n_processed_files > BATCH_SIZE)
user_data);
}
-/* Stolen from libtracker-common */
-static GList *
-string_list_to_gslist (gchar **strv)
-{
- GList *list;
- gsize i;
-
- list = NULL;
-
- for (i = 0; strv[i]; i++)
- list = g_list_prepend (list, g_strdup (strv[i]));
-
- return g_list_reverse (list);
-}
-
/* Stolen from libtracker-sparql */
static gchar *
sparql_escape_string (const gchar *literal)
GVariant *reply;
GVariant *r;
GVariantIter iter;
- gchar **result;
GError *error = NULL;
gint i, n;
+ GtkSearchHit *hit;
tracker = GTK_SEARCH_ENGINE_TRACKER (user_data);
r = g_variant_get_child_value (reply, 0);
g_variant_iter_init (&iter, r);
n = g_variant_iter_n_children (&iter);
- result = g_new0 (gchar *, n + 1);
+ hit = g_new (GtkSearchHit, n);
+ hits = NULL;
for (i = 0; i < n; i++)
{
GVariant *v;
v = g_variant_iter_next_value (&iter);
strv = g_variant_get_strv (v, NULL);
- result[i] = (gchar*)strv[0];
+ hit[i].uri = (gchar*)strv[0];
+ hit[i].info = NULL;
g_free (strv);
+ hits = g_list_prepend (hits, &hit[i]);
}
- /* We iterate result by result, not n at a time. */
- hits = string_list_to_gslist (result);
_gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (tracker), hits);
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker));
g_list_free (hits);
- g_free (result);
+ g_free (hit);
g_variant_unref (reply);
g_variant_unref (r);